//
//  Test_keyPath.h
//  TestMac
//
//  Created by 小星星吃KFC on 2021/6/3.
//

#import <Foundation/Foundation.h>
#import "Person.h"

static void test_keyPath(void){
    
    NSLog(@" = %d, = %lf",1 << 0,pow(2, 0)); //1
    NSLog(@" = %d, = %lf",1 << 1,pow(2, 1)); //2
    NSLog(@" = %d, = %lf",1<<2,pow(2, 2));//4
    NSLog(@" = %d, = %lf",1<<3,pow(2, 3));// 8
    NSLog(@" = %d, = %lf",1<<4,pow(2, 4));// 16
    
    NSLog(@" = %d,",(1<<2 | 1<<3 | 1<<4 ) &  1<<3 );//1<<3
    NSLog(@"compare = %ld",[@"1.9" compare:@"1.10"]);// 1.9 > 1.10
    NSLog(@"compare = %ld",[@"1.9" compare:@"1.1"]);
    NSLog(@"compare = %ld",[@"1.9" compare:@"1.8"]); // 1.9 > 1.8
    
    NSLog(@"compare = %ld",[@"1.9" compare:@"1.10" options:NSNumericSearch]);// 1.9 > 1.10
    NSLog(@"compare = %ld",[@"1.9" compare:@"1.1" options:NSNumericSearch]);
    NSLog(@"compare = %ld",[@"1.9" compare:@"1.8" options:NSNumericSearch]); // 1.9 > 1.8
    
    
    NSLog(@"compare = %ld",[@"1.9" compare:@"1.10" options:NSNumericSearch]);
    NSLog(@"compare = %ld",[@"1.8" compare:@"1.9" options:NSNumericSearch]);
    
    
    //        NSString *s1 = @"";
    //        s1 = nil;
    //        s1 = @[];
    //        NSLog(@"class = %@",s1.class);
    
    //        NSLog(@"len = %d",s1.length);
    
    
    //        NSLog(@"%@",[@"" componentsSeparatedByString:@","]);
    
    
    //        NSLog(@"sortedArrayHint = %@",[@[@"1",@"2"] sortedArrayHint]);
    
    //
    //        NSArray *arr = @[@"4",@"3"];
    //
    //      NSArray *arr1 = [@[@"3",@"4"] sortedArrayUsingFunction:dateSort1 context:nil hint:[@[@"1",@"2"] sortedArrayHint]];
    ////        NSArray *arr1 = [arr sortedArrayUsingFunction:dateSort1 context:nil];
    //
    //        NSLog(@"arr = %@",arr);
    //        NSLog(@"arr1 = %@",arr1);
    //
    //        NSString *str = @"1/2/3/";
    //        NSLog(@"%c", [str characterAtIndex:str.length-1]);
    //
    //
    //
    //        char str2[] = "abc"; // a b
    //
    //
    //        NSString *string = @"1/2";
    //
    //        unichar single = [string characterAtIndex:string.length-1]; //当前输入的字符
    //        if (single == '/') {
    //            NSLog(@"char 相同");
    //        }
    //
    //
    
    //        id json = [NSJSONSerialization JSONObjectWithData:[@[@"1",@"2"] sortedArrayHint] options:NSJSONReadingMutableContainers error:nil];
    //
    //        NSLog(@"json = %@",json);
    
    
    //        NSArray *arr1 = @[@"1",@"2",@"3",@"4",@"5"];
    //
    //        NSMutableOrderedSet *orSet = [NSMutableOrderedSet orderedSet];
    //
    //        [orSet addObjectsFromArray:arr1];
    //
    //        [orSet enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    //            NSLog(@"有序: %@",obj);
    //        }];
    //
    //        NSMutableSet *set = [NSMutableSet set];
    //
    //        [set addObjectsFromArray:arr1];
    //        [set enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
    //                    NSLog(@"无序:%@",obj);
    //    }];
    
    
    
    
    //        NSString *str2 = @"";
    //        NSLog(@"isEqualToString = %d",[str isEqualToString:str2]);
    
    NSMutableArray<Person *> *array = [NSMutableArray array];
    {
        Person *p = Person.alloc.init;
        p.name = @"jim2";
        p.age = 2;
        
        Dog *dog =  Dog.alloc.init;
        dog.name = @"jim2-dog";
        p.dog = dog;
        [array addObject:p];
        [array addObject:p];
    }
    {
        Person *p = Person.alloc.init;
        p.name = @"jim3";
        p.age = 1;
        [array addObject:p];
    }
    {
        Person *p = Person.alloc.init;
        p.name = @"jim4";
        p.age = 3;
        [array addObject:p];
    }
    {
        Person *p = Person.alloc.init;
        p.name = @"jim1";
        p.age = 3;
        [array addObject:p];
    }
    
    //        {
    //            Person *p = Person.alloc.init;
    //            p.name = @"tom";
    //            p.age = 4;
    //            [array addObject:p];
    //        }
    //        {
    //            Person *p = Person.alloc.init;
    //            p.name = @"joM";
    //            p.age = 4;
    //            [array addObject:p];
    //        }
    
    /**
     https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/CollectionOperators.html#//apple_ref/doc/uid/20002176-BAJEAIEE
     
     /// 注意前面有个 @"@
     [NSString stringWithFormat:@"@%@.name",NSUnionOfSetsKeyValueOperator];
     
     
     
     当调用KVC时key值为空时，就会抛出这个异常。
     FOUNDATION_EXPORT NSExceptionName const NSUndefinedKeyException;
     
     
     // NSKeyValueCoding中的运算符
     NSKeyValueOperator const NSAverageKeyValueOperator; // 求平均值
     NSKeyValueOperator const NSCountKeyValueOperator; // 统计总数
     NSKeyValueOperator const NSDistinctUnionOfArraysKeyValueOperator; // 获取嵌套数组中不同的值
     NSKeyValueOperator const NSDistinctUnionOfObjectsKeyValueOperator; // 获取不同的值
     NSKeyValueOperator const NSDistinctUnionOfSetsKeyValueOperator; // 获取嵌套集合中不同的值
     NSKeyValueOperator const NSMaximumKeyValueOperator; // 获取最大值
     NSKeyValueOperator const NSMinimumKeyValueOperator; // 获取最小值
     NSKeyValueOperator const NSSumKeyValueOperator; // 求和
     NSKeyValueOperator const NSUnionOfArraysKeyValueOperator; // 获取嵌套数组中的值，不去重
     NSKeyValueOperator const NSUnionOfObjectsKeyValueOperator; // 获取所有的值，不去重
     NSKeyValueOperator const NSUnionOfSetsKeyValueOperator; // 获取嵌套集合中的值，不去重
     
     
     
     */
    
    
    NSLog(@"sum.age ==== %@",[array valueForKeyPath:@"@sum.age"]);
    /// valueForKeyPath,valueForKey 一层效果一样,但最好使用前者
    NSLog(@"一级 ==== %@",[array valueForKeyPath:@"name"]);
    NSLog(@"一级 ==== %@",[array valueForKey:@"name"]);
    NSLog(@"多层查找 ==== %@",[array valueForKeyPath:@"dog.name"]);
    
    /// 去重
    /// @distinctUnionOfObjects.self  字符串数组
    /// @distinctUnionOfObjects.name  对象数组
    
    /// distinctUnionOfSets 目前不行
    NSLog(@"不同的联盟,去重,与 valueForKeyPath 效果一样 ==== %@",[array valueForKeyPath:@"@distinctUnionOfObjects.name"]);
    NSLog(@"OrderedSet ==== %@",[array mutableOrderedSetValueForKeyPath:@"name"]);
    /// 不会去重
    NSLog(@"mutableSetValue ==== %d",[[array mutableSetValueForKeyPath:@"name"] isKindOfClass:NSMutableOrderedSet.class]);
    
    NSLog(@"mutableSetValueForKey ==== %@",[array mutableSetValueForKey:@"name"]);
    
    
    NSLog(@"不同的联盟Arrays ==== %@",[@[array] valueForKeyPath:@"@distinctUnionOfArrays.name"]);
    
    
    //        Person *p = Person.alloc.init;
    //        p.name = @"jim2";
    //        p.age = 2;
    //
    //
    //        NSSet *setOfobjectsSets = [NSSet setWithObjects:p, nil];
    //        NSLog(@"不同的联盟Set ==== %@",[setOfobjectsSets valueForKeyPath:@"@distinctUnionOfSets.name"]);
    
    
    
    
    //        NSLog(@"不同的联盟Arrays2 ==== %@",[array mutableArrayValueForKeyPath:[NSString stringWithFormat:@"@%@.name",NSDistinctUnionOfArraysKeyValueOperator]]);
    
    
    
    
    //        NSLog(@"NSDistinctUnionOfArraysKeyValueOperator = %@",NSDistinctUnionOfArraysKeyValueOperator);
    //
    //        NSLog(@"NSUnionOfSetsKeyValueOperator = %@",NSUnionOfSetsKeyValueOperator);
    //
    //
    //        {
    //            NSArray *array2 = @[@"1",@"1",@"1",@"2",@"2",@"3",@"3",@"4",@"5",@"5"];
    //                NSArray *array1 = @[@"3",@"4",@"5",@"5",@"6",@"6",@"7",@"8",@"8"];
    //
    //
    //            NSMutableSet *set1 = [NSMutableSet setWithArray:array1];
    //            NSMutableSet *set2 = [NSMutableSet setWithArray:array2];
    //
    //
    //            [set1 unionSet:set2];
    //            NSLog(@"不同的联盟Sets ==== %@",[set1 valueForKeyPath:[NSString stringWithFormat:@"@%@.name",NSUnionOfSetsKeyValueOperator]]);
    //
    //        }
    
    //        NSLog(@"不同的联盟Sets ==== %@",[array mutableSetValueForKeyPath:[NSString stringWithFormat:@"%@.name",NSUnionOfSetsKeyValueOperator]]);
    {
        
        NSArray *array = @[@"1",@"1",@"1",@"2",@"2",@"3",@"3",@"4",@"5",@"5"];
        NSArray *array1 = @[@"3",@"4",@"5",@"5",@"6",@"6",@"7",@"8",@"8"];
        NSArray *unionArray = @[array,array1];
        
        
        NSArray *distinctUnionOfArrays = [unionArray valueForKeyPath:@"@distinctUnionOfArrays.self"];
        NSLog(@"多个数组去重后 = %@",distinctUnionOfArrays);
        
        
        
    }
    
    {
        
        //            [@[array] valueForKeyPath:@"@distinctUnionOfArrays.name"]; // "iPhone 5", "iPad Mini", "MacBook Pro"
        
        
        
    }
    NSLog(@"联盟 ==== %@",[array valueForKeyPath:@"@unionOfObjects.name"]);
    
    
    NSLog(@"mutableOrderedSetValueForKey ==== %@",[array mutableOrderedSetValueForKey:@"name"]);
    {
        /// 进行实例方法的调用
        NSArray *array = @[@"name", @"w", @"aa", @"ZXPing"];
        /// 转大写
        NSLog(@"大写 = %@", [array valueForKeyPath:@"uppercaseString"]);
        NSLog(@"小写 = %@", [array valueForKeyPath:@"lowercaseString"]);
        NSLog(@"length = %@", [array valueForKeyPath:@"length"]);
        
        //        NSString *str = @"AA";
        //            @"AAA" distinctUnionOfObje
        
    }
    
}


